import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import router from './router'
import store from './store'
import './api/mock.js'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import './assets/less/index.less'


import axios from 'axios'

Vue.config.productionTip = false

Vue.use(ElementUI);
Vue.use(VueRouter);

Vue.prototype.$axios = axios
//axios.defaults.baseURL = 'http://127.0.0:3333/'

//路由的监听,相当于拦截器(导航守卫)
router.beforeEach((to, from, next) => {
  store.commit('getToken')
  const token = store.state.user.token
  if (!token && to.name !== 'login') {
    next({name: 'login'} )
  }else if(token && to.name === 'login'){
    next( {name: 'home'} )
  } 
  else{
    next()
  }
})

new Vue({
  router,
  store,
  render: h => h(App), 
  //解决home界面白屏问题
  created(){
    store.commit('addMenu', router)
  }
}).$mount('#app')
